﻿using System;
using System.Collections.Generic;
using netDxf;
using netDxf.Entities;
using netDxf.Header;
using netDxf.Tables;

namespace TestDxfDocument.toolingsystem
{
    public class ToolingSystem
    {
        /// <summary>
        /// JT柄  GB/T 10944.1-2013
        /// </summary>
        /// <param name="data"></param>
        public static void JTToolShank(Dictionary<string, Object> data)
        {
            DxfDocument dxf = new DxfDocument();
            
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓

            double pi = Math.PI;

            //尺寸参数，来自于GB/T 10944.1-2013 表1
            var aorUShankSize = AorUShankSize.getparam("JT40");
            double b = aorUShankSize.B;
            double d1 = aorUShankSize.D1;
            double d2 = aorUShankSize.D2;
            double d3 = aorUShankSize.D3;
            double d4 = aorUShankSize.D4;
            double d5 = aorUShankSize.D5;
            double d6 = aorUShankSize.D6;
            double d7 = aorUShankSize.D7;
            double d11 = aorUShankSize.D11;
            double e = aorUShankSize.E;
            double f = aorUShankSize.F;
            double j = aorUShankSize.J;
            double l1 = aorUShankSize.L1;
            double l2 = aorUShankSize.L2;
            double l3 = aorUShankSize.L3;
            double l4 = aorUShankSize.L4;
            double l5 = aorUShankSize.L5;
            double l6 = aorUShankSize.L6;
            double l7 = aorUShankSize.L7;
            double r1 = aorUShankSize.R1;
            double r2 = aorUShankSize.R2;
            double r3 = aorUShankSize.R3;
            double u = aorUShankSize.U;
            double v = aorUShankSize.V;
            double x = aorUShankSize.X;
            double y = aorUShankSize.Y;
            double a = 8.29722 * pi / 180; //8°17′50″换算

            String path = "E:\\dxfp\\BTToolShank.dxf";

            Vector2 p0 = new Vector2(0, 0);
            Vector2 p1 = fun.Polar(p0, 0, u);
            Vector2 p2 = new Vector2(p1.X, p0.Y + d5 / 2);
            Vector2 p6 = new Vector2(p0.X + v, p0.Y + d4 / 2);
            Vector2 p5 = new Vector2(p6.X + x / 2, p6.Y);
            Vector2 p3 = new Vector2(p5.X + ((d5 - d4) / 2) * Math.Tan(pi / 6), p2.Y);
            Vector2 p7 = new Vector2(p6.X - x / 2, p6.Y);
            Vector2 p9 = new Vector2(p7.X - ((d5 - d4) / 2) * Math.Tan(pi / 6), p2.Y);
            Vector2 p10 = new Vector2(p0.X + y, p9.Y);
            Vector2 p11 = new Vector2(p10.X, p0.Y + d1 / 2 - d1 / 40);
            Vector2 p12 = new Vector2(p0.X, p11.Y);
            Vector2 p13 = new Vector2(p0.X, p0.Y + d1 / 2);
            Vector2 p15 = new Vector2(p0.X - l1, p13.Y - l1 * Math.Sin(a));

            Vector2 p2p = new Vector2(p2.X, -p2.Y);
            Vector2 p5p = new Vector2(p5.X, -p5.Y);
            Vector2 p3p = new Vector2(p3.X, -p3.Y);
            Vector2 p7p = new Vector2(p7.X, -p7.Y);
            Vector2 p9p = new Vector2(p9.X, -p9.Y);
            Vector2 p10p = new Vector2(p10.X, -p10.Y);
            Vector2 p11p = new Vector2(p11.X, -p11.Y);
            Vector2 p12p = new Vector2(p12.X, -p12.Y);
            Vector2 p13p = new Vector2(p13.X, -p13.Y);
            Vector2 p15p = new Vector2(p15.X, -p15.Y);

            Vector2 p30 = new Vector2(p10.X, p0.Y + b / 2);
            Vector2 p31 = new Vector2(p9.X, p30.Y);
            Vector2 p32 = new Vector2(p7.X, p30.Y);
            Vector2 p33 = new Vector2(p5.X, p30.Y);
            Vector2 p34 = new Vector2(p3.X, p30.Y);
            Vector2 p35 = new Vector2(p2.X, p30.Y);
            
            Vector2 p30p = new Vector2(p30.X, -p30.Y);
            Vector2 p31p = new Vector2(p31.X, -p31.Y);
            Vector2 p32p = new Vector2(p32.X, -p32.Y);
            Vector2 p33p = new Vector2(p33.X, -p33.Y);
            Vector2 p34p = new Vector2(p34.X, -p34.Y);
            Vector2 p35p = new Vector2(p35.X, -p35.Y);
            
            LwPolyline outline = Pus.AddPointsToLwPolyLine(p2, p3, p5, p7, p9, p10, p11, p12, p13, p15, p15p, p13p, p12p,
                p11p, p10p, p9p, p7p, p5p, p3p, p2p, p2);
            fun.drawBLine(dxf, Pus.GetV2ArrayFromLwPolyline(outline), 0.25, contour);
            
            Vector2[] outline1 = {p13, p13p};
            fun.drawPline(dxf, outline1,0.15, contour);
            
            fun.drawBLine(dxf, Pus.GetV2ArrayFromLwPolyline(Pus.AddPointsToLwPolyLine(p10, p10p)), 0.25, contour);

            Vector2[] outline2 = {p30, p35};
            commonfun.drawLineWithWidth(outline2,dxf,0.25,contour);
            commonfun.drawRotateXLineWithWidth(outline2,dxf,0.25,contour);
            Vector2[] outline3 = {p9, p31};
            commonfun.drawLineWithWidth(outline3,dxf,0.25,contour);
            commonfun.drawRotateXLineWithWidth(outline3,dxf,0.25,contour);
            Vector2[] outline4 = {p7, p32};
            commonfun.drawLineWithWidth(outline4,dxf,0.25,contour);
            commonfun.drawRotateXLineWithWidth(outline4,dxf,0.25,contour);
            Vector2[] outline5 = {p5, p33};
            commonfun.drawLineWithWidth(outline5,dxf,0.25,contour);
            commonfun.drawRotateXLineWithWidth(outline5,dxf,0.25,contour);
            Vector2[] outline6 = {p3, p34};
            commonfun.drawLineWithWidth(outline6,dxf,0.25,contour);
            commonfun.drawRotateXLineWithWidth(outline6,dxf,0.25,contour);

            Line cenLine1 = new Line(fun.Polar(p0, pi, l1 + 1), fun.Polar(p0, 0, u + 1));
            cenLine1.Linetype = Linetype.Center;
            cenLine1.Layer = centre;
            dxf.AddEntity(cenLine1);
            Line cenLine2 = new Line(fun.Polar(p0, 0.5 * pi, d1 / 2 + 1), fun.Polar(p0, 1.5 * pi, d1 / 2 + 1));
            cenLine2.Linetype = Linetype.Center;
            cenLine2.Layer = centre;
            dxf.AddEntity(cenLine2);
            Line cenLine3 = new Line(new Vector2(p6.X, d5 / 2 + 1), new Vector2(p6.X, -d5 / 2 - 1));
            cenLine3.Linetype = Linetype.Center;
            cenLine3.Layer = centre;
            dxf.AddEntity(cenLine3);

            Vector2 p20 = new Vector2(p15.X, p0.Y + d11 / 2);
            Vector2 p21 = new Vector2(p20.X + r2, p0.Y + d2 / 2);
            Vector2 p22 = new Vector2(p15.X + l4, p21.Y);
            Vector2 p23 = new Vector2(p22.X + 1, p22.Y - 0.866);
            Vector2 p24 = new Vector2(p15.X + l3, p23.Y);
            Vector2 p25 = new Vector2(p24.X + d7 / 2 * Math.Tan(pi / 6), p0.Y);
            Vector2 p26 = new Vector2(p15.X + l2, p22.Y);
            
            Vector2 p20p = new Vector2(p20.X, -p20.Y);
            Vector2 p21p = new Vector2(p21.X, -p21.Y);
            Vector2 p22p = new Vector2(p22.X, -p22.Y);
            Vector2 p23p = new Vector2(p23.X, -p23.Y);
            Vector2 p24p = new Vector2(p24.X, -p24.Y);
            Vector2 p26p = new Vector2(p26.X, -p26.Y);

            LwPolyline holeOutline =
                Pus.AddPointsToLwPolyLine(p20, p21, p22, p23, p24, p25, p24p, p23p, p22p, p21p, p20p);
            fun.drawBLine(dxf, Pus.GetV2ArrayFromLwPolyline(holeOutline), 0.25, contour);
            LwPolyline threadHoleline =
                Pus.AddPointsToLwPolyLine(p26, p26p);
            fun.drawBLine(dxf, Pus.GetV2ArrayFromLwPolyline(threadHoleline), 0.25, contour);
            Vector2[] holeLine1 = {p21, p21p};
            fun.drawPline(dxf, holeLine1, 0.15, contour);
            Vector2[] holeLine2 = {p22, p22p};
            fun.drawPline(dxf, holeLine2, 0.15, contour);
            Vector2[] holeLine3 = {p23, p23p};
            fun.drawPline(dxf, holeLine3, 0.15, contour);
            Vector2[] holeLine4 = {p24, p24p};
            fun.drawPline(dxf, holeLine4, 0.15, contour);
            Vector2[] holeLine5 = {p26, p26p};
            fun.drawPline(dxf, holeLine5, 0.15, contour);
            Vector2[] holeLine6 = {p22, p26};
            fun.drawPlinetype(dxf, holeLine6, 0.15, refin,Linetype.Dashed);
            Vector2[] holeLine7 = {p22p, p26p};
            fun.drawPlinetype(dxf, holeLine7, 0.15, refin,Linetype.Dashed);
            
            #region 左视图部分

            Vector2 z1 = fun.Polar(p0, 0, l1 * 1.5);
            Vector2 z2 = new Vector2(z1.X - l5, z1.Y + b / 2);
            Vector2 z3 = new Vector2(z2.X, z1.Y - b / 2);
            Vector2 z4 = new Vector2(z1.X + l6, z2.Y);
            Vector2 z5 = new Vector2(z4.X, z3.Y);

            Line cenLine4 = new Line(fun.Polar(z1, pi, d5 / 2 + 1), fun.Polar(z1, 0, d5 / 2 + 1));
            cenLine4.Linetype = Linetype.Center;
            cenLine4.Layer = centre;
            dxf.AddEntity(cenLine4);
            Line cenLine5 = new Line(fun.Polar(z1, 0.5 * pi, d5 / 2 + 1), fun.Polar(z1, 1.5 * pi, d5 / 2 + 1));
            cenLine5.Linetype = Linetype.Center;
            cenLine5.Layer = centre;
            dxf.AddEntity(cenLine5);

            Circle circle1 = new Circle(z1, d5 / 2);
            Circle circle2 = new Circle(z1, d1 / 2);
            Circle circle3 = new Circle(z1, p15.Y);
            Circle circle4 = new Circle(z1, p20.Y);
            Circle circle5 = new Circle(z1, p21.Y);
            Circle circle6 = new Circle(z1, p23.Y);

            List<Vector2> pointDic1 = commonfun.getPointOFLineandCircle(z2, z4, circle1);
            Vector2 z6 = pointDic1[0];
            Vector2 z8 = pointDic1[1];
            List<Vector2> pointDic2 = commonfun.getPointOFLineandCircle(z3, z5, circle1);
            Vector2 z7 = pointDic2[0];
            Vector2 z9 = pointDic2[1];
            
            Arc arc1 = fun.DrawArc(z8, z1, z6);
            commonfun.arcWithWidth(arc1,dxf,0.25,contour);
            Arc arc2 = fun.DrawArc(z7, z1, z9);
            commonfun.arcWithWidth(arc2,dxf,0.25,contour);
            
            commonfun.circleWithWidth(circle2,dxf,0.25,contour);
            commonfun.circleWithWidth(circle3,dxf,0.25,contour);
            commonfun.circleWithWidth(circle4,dxf,0.25,contour);
            commonfun.circleWithWidth(circle5,dxf,0.1,contour);
            commonfun.circleWithWidth(circle6,dxf,0.1,contour);
            
            LwPolyline zkeyLine1 = Pus.AddPointsToLwPolyLine(z6, z2, z3, z7);
            fun.drawBLine(dxf, Pus.GetV2ArrayFromLwPolyline(zkeyLine1), 0.25, contour);
            LwPolyline zkeyLine2 = Pus.AddPointsToLwPolyLine(z8, z4, z5, z9);
            fun.drawBLine(dxf, Pus.GetV2ArrayFromLwPolyline(zkeyLine2), 0.25, contour);
            
            Vector2 z11 = new Vector2(z1.X + j, z1.Y - j);
            Vector2 z10 = new Vector2(z11.X, - Math.Sqrt(d5 * d5 / 4 - j * j));
            List<Vector2> pointDic3 = commonfun.getPointOFLineandCircle(z11, fun.Polar(z11, 0, d5), circle1);
            Vector2 z12 = pointDic3[1];
            
            LwPolyline zcontourLine10 = Pus.AddPointsToLwPolyLine(z10, z11, z12);
            fun.drawBLine(dxf, Pus.GetV2ArrayFromLwPolyline(zcontourLine10), 0.25, contour);

            #endregion
            
            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
        }

        public static void BTToolShank(Dictionary<string, Object> data)
        {
            DxfDocument dxf = new DxfDocument();
            
            Layer contour = new Layer("contour") { Color = AciColor.Cyan };    //轮廓层
            Layer centre = new Layer("centre") { Color = AciColor.Yellow };    //中心线
            Layer refin = new Layer("refin") { Color = AciColor.Red };    //基准线/内轮廓

            double pi = Math.PI;
            String path = "E:\\dxfp\\specialCurve.dxf";
            

            dxf.DrawingVariables.AcadVer = DxfVersion.AutoCad2007;
            dxf.Save(path);
            
        }
    }
}